package com.ruoyi.system.controller;

import com.ruoyi.system.DTO.Reques.vms.SkuDTO;
import com.ruoyi.system.DTO.Reques.vms.SkuSearchDTO;
import com.ruoyi.system.DTO.Reques.vms.SkuUpdateDto;
import com.ruoyi.system.pojo.vms.SkuEntity;
import com.ruoyi.system.service.SkuService;
import com.ruoyi.system.utils.MinioUtil;
import com.ruoyi.system.utils.Result;
import com.ruoyi.system.vo.vms.PageResult;
import com.ruoyi.system.vo.vms.PageVO;
import com.ruoyi.system.vo.vms.SkuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author JourWon
 * @date Created on 2025/5/13
 */
@RestController
@RequestMapping("/vm-service")
@Slf4j
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 商品管理列表 索
     * @param pageIndex
     * @param pageSize
     * @param skuName
     * @param classId
     * @return
     */
    @GetMapping("/sku/search")
    public ResponseEntity<PageVO<SkuVO>> search(
            @RequestParam(required = false) Integer pageIndex,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String skuName,
            @RequestParam(required = false) Integer classId) {

        SkuSearchDTO searchDTO = new SkuSearchDTO();
        searchDTO.setPageIndex(pageIndex);
        searchDTO.setPageSize(pageSize);
        searchDTO.setSkuName(skuName);
        searchDTO.setClassId(classId);

        return ResponseEntity.ok(skuService.search(searchDTO));
    }

    /**
     * 创建商品
     * @param skuDTO
     * @return
     */
    @PostMapping("/sku")
    public ResponseEntity<Boolean> create(@RequestBody @Valid SkuDTO skuDTO) {
        return ResponseEntity.ok(skuService.create(skuDTO));
    }


    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/sku/fileUpload")
    public ResponseEntity<?> fileUpload(@RequestParam("fileName") MultipartFile file) {
        try {
            String fileName = minioUtil.upload(file);
            return ResponseEntity.ok(fileName);
        } catch (Exception e) {
            // 记录具体错误原因
            log.error("文件上传失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("上传失败: " + e.getMessage());
        }
    }

    @PutMapping("/sku/{skuId}")
    public boolean updateSku(@PathVariable Long skuId,
                             @RequestBody SkuUpdateDto updateDto) {
        return skuService.updateSku(skuId, updateDto);
    }

    @PostMapping("/sku/upload")
    public Integer uploadSkus(@RequestParam("file") MultipartFile file,
                              @RequestParam(value = "overwrite", defaultValue = "false") boolean overwrite) {
        return skuService.importSkus(file, overwrite);
    }

}
